package cn.itcast.service.impl;

import cn.itcast.dao.cargo.FactoryDao;
import cn.itcast.domain.cargo.ContractProduct;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.cargo.FactoryService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;


import java.util.List;
import java.util.UUID;

@Service(timeout = 3000000)
public class FactoryServiceImpl implements FactoryService {
    @Autowired
    private FactoryDao factoryDao;
    //分页
    @Override
    public PageInfo<Factory> findByPage(FactoryExample factoryExample, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Factory> list = factoryDao.selectByExample(factoryExample);
        //返回查询内容
        return new PageInfo<>(list);
    }

    //查尊所有
    @Override
    public List<Factory> findAll(FactoryExample factoryExample) {
        return factoryDao.selectByExample(factoryExample);
    }

    //根据id查询
    @Override
    public Factory findById(String id) {
        return factoryDao.selectByPrimaryKey(id);
    }

    //保存
    @Override
    public void save(Factory factory) {
        //设置id
        factory.setId(UUID.randomUUID().toString());

        factoryDao.insertSelective(factory);

    }

    //跟新
    @Override
    public void update(Factory factory) {
        factoryDao.updateByPrimaryKeySelective(factory);
    }

    //根据id删除
    @Override
    public void delete(String id) {

        factoryDao.deleteByPrimaryKey(id);
    }

    @Override
    public Factory findByName(String factoryName) {
        //创建查询对象
        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria criteria = factoryExample.createCriteria();
        criteria.andFactoryNameEqualTo(factoryName);

        //查询
        List<Factory> factoryList = factoryDao.selectByExample(factoryExample);
        return factoryList != null && factoryList.size() >0 ? factoryList.get(0) : null;
    }
}
